package com.aosiding.service.impl;

import com.aosiding.pojo.Order;
import com.aosiding.pojo.Product;
import com.aosiding.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;

/**
 * @version 1.0
 * @Author AO SIDING
 * Created on 2022/9/17 17:10
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    //Ribbon 负载均衡器
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Override
    public Order selectOrderById(Integer id) {

        return new Order(
                id,
                "order-001",
                "中国",
                31994D,
                selectProductListbyLoadBalancerAnnotation());
    }

    /**
     * 三种方式调用
     * 1.selectProductByDiscoveryClient
     * 2.selectProductListByLoadBanlancerClient
     * 3.selectProductListbyLoadBalancerAnnotation 使用注解的方式最常用,最简单
     * @return
     */
    private List<Product> selectProductByDiscoveryClient() {
        StringBuffer sb = null;
        //获取服务列表
        List<String> services = discoveryClient.getServices();
        if (CollectionUtils.isEmpty(services)) {
            return null;
        }
        //根据服务名称获取服务
        List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
        if (CollectionUtils.isEmpty(instances)) {
            return null;
        }

        ServiceInstance serviceInstance = instances.get(0);
        sb = new StringBuffer();
        sb.append("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/list");

        //ResponseEntity 封装了返回数据
        ResponseEntity<List<Product>> response = restTemplate.exchange(
                sb.toString(),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {
                }
        );
        return response.getBody();
    }

    private List<Product> selectProductListByLoadBanlancerClient() {
        StringBuffer sb = null;
        ServiceInstance choose = loadBalancerClient.choose("service-provider");
        if (choose == null) {
            return null;
        }
        sb = new StringBuffer();
        sb.append("http://" + choose.getHost() + ":" + choose.getPort() + "/product/list");

        ResponseEntity<List<Product>> response = restTemplate.exchange(
                sb.toString(),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {
                }
        );
        return response.getBody();

    }

    private List<Product> selectProductListbyLoadBalancerAnnotation(){
        ResponseEntity<List<Product>> response = restTemplate.exchange(
                "http://service-provider/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {
                }
        );
        return response.getBody();
    }
}
